package com.xm.xmapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.Constants;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.CustomLayout;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Get_user_zhimascoreRequest;
import com.xm.xmapp.request.bean.Zhima_AuthorizeRequest;
import com.xm.xmapp.response.bean.Get_user_zhimascoreRsponse;
import com.xm.xmapp.response.bean.ResponseZhimaAuthorize;

public class AuthorizeZhimaIsActivity extends BaseActivity implements OnClickListener {

	private Get_user_zhimascoreRsponse bean;
	private TextView marks, time, status;
	private CustomLayout cl_what, cl_expanl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhimaisauthorize);
	}

	@Override
	public void initView() {
		marks = (TextView) findViewById(R.id.marks);
		time = (TextView) findViewById(R.id.time);
		status = (TextView) findViewById(R.id.status);
		cl_what = (CustomLayout) findViewById(R.id.cl_what);
		cl_expanl = (CustomLayout) findViewById(R.id.cl_expanl);
	}

	@Override
	protected void onResume() {
		super.onResume();
		getScore();
	}

	/**
	 * 重新获取芝麻信用分
	 */
	private void getScore() {
		Get_user_zhimascoreRequest requestBean = new Get_user_zhimascoreRequest();
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				bean = JSON.parseObject(response, Get_user_zhimascoreRsponse.class);
				if (bean != null) {
					marks.setText(bean.zhima_score);
					time.setText("评估时间：" + bean.zhima_authorize_time);
					status.setText(bean.zhima_rate);
				}
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

	@Override
	public void initData() {
		setMyTitle("芝麻信用");
		bean = (Get_user_zhimascoreRsponse) getIntent().getSerializableExtra("bean");
		if (bean != null) {
			marks.setText(bean.zhima_score);
			time.setText("评估时间：" + bean.zhima_authorize_time);
			status.setText(bean.zhima_rate);
		}
	}

	@Override
	public void initEvent() {
		cl_what.setOnClickListener(this);
		cl_expanl.setOnClickListener(this);
		findViewById(R.id.submit).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cl_what:
			if (bean != null) {
				intent = new Intent(MyAppcation.getMyAppcation(), ShowURLActivity.class);
				intent.putExtra("url", bean.zhima_url);
				intent.putExtra("title", "芝麻信用");
				startActivity(intent);
			}
			break;
		case R.id.cl_expanl:
			intent = new Intent(MyAppcation.getMyAppcation(), ShowURLActivity.class);
			intent.putExtra("url", Constants.Zhima_discount_note);
			intent.putExtra("title", "芝麻优惠说明");
			startActivity(intent);
			break;
		case R.id.submit:
			getZhimaAuthorize();
			break;
		default:
			break;
		}
	}

	/**
	 * 跳转芝麻认证
	 */
	protected void getZhimaAuthorize() {
		Zhima_AuthorizeRequest requestBean = new Zhima_AuthorizeRequest();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				ResponseZhimaAuthorize bean = JSON.parseObject(response, ResponseZhimaAuthorize.class);
				intent = new Intent(MyAppcation.getMyAppcation(), ShowURLActivity.class);
				intent.putExtra("url", bean.authorize_url);
				intent.putExtra("title", "芝麻信用");
				startActivity(intent);
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}
}
